package cn.qdhlm.frontendprocessor.common.config;

import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Name: HttpClientConfig
 * @Description: HttpClientConfig
 * @Author: jiangcs
 */
@Configuration
public class HttpClientConfig {
    @Bean
    public CloseableHttpClient httpClient() {
        RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.create();
        // http
        registryBuilder.register("http", new PlainConnectionSocketFactory());
        // https
        registryBuilder.register("https", SSLConnectionSocketFactory.getSocketFactory());
        PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager(registryBuilder.build());
        // 设置最大的连接数【默认，总连接数不能超过20】
        poolingHttpClientConnectionManager.setMaxTotal(1000);
        // 设置每个路由的基础连接数【默认，每个路由基础上的连接不超过2个】
        poolingHttpClientConnectionManager.setDefaultMaxPerRoute(800);
        return HttpClients.custom()
                .setConnectionManager(poolingHttpClientConnectionManager)
                .build();
    }
}
